package com.chookss.home.knowledge.adaper;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chookss.MyApp;
import com.chookss.R;
import com.chookss.Urls;
import com.chookss.WebCommonActivity;
import com.chookss.base.BaseViewHolderEx;
import com.chookss.base.StaticClass;
import com.chookss.home.NavigationBaseActivity;
import com.chookss.home.audit.PaySlipActivity;
import com.chookss.home.audit.WorkCardActivity;
import com.chookss.home.audit.clockIn.ClockInAskHolidayActivity;
import com.chookss.home.audit.clockIn.ClockInHomeActivity;
import com.chookss.home.audit.overtime.OvertimeActivity;
import com.chookss.home.audit.project.ProjectManagementActivity;
import com.chookss.home.audit.reimbursement.ReimbursementActivity;
import com.chookss.home.audit.resign.ResignHomeActivity;
import com.chookss.home.audit.seal.SealHomeActivity;
import com.chookss.home.audit.shop.ShopActivity;
import com.chookss.home.audit.upgrade.UpgradeHomeActivity;
import com.chookss.home.entity.NavigationEntity;
import com.chookss.home.inspection.InspectionHomeActivity;
import com.chookss.home.interview.InterviewRecordsActivity;
import com.chookss.home.interview.InvitationRegisterActivity;
import com.chookss.home.publish.PublishLogsActivity;
import com.chookss.home.publish.PublishNewsActivity;
import com.chookss.home.running.RunningHomeActivity;
import com.chookss.home.tuwen.PublishTuwensActivity;
import com.chookss.home.workbench.WorksActivity;
import com.chookss.tools.GlideUtils;
import com.chookss.tools.MyEvent;
import com.chookss.tools.MyToast;
import com.chookss.tools.NavigationTools;
import com.chookss.tools.SignUtils2;
import com.chookss.tools.Utils;
import com.chookss.tools.okgoconfig.MyHttpRequest;
import com.chookss.tools.okgoconfig.OkGoUtil;
import com.chookss.tools.okgoconfig.callback.JsonCallback;
import com.chookss.video.VideoBaseActivity;
import com.johnrambo.ktea.ktExtends.ShareUtils;
import com.lzy.okgo.model.Response;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class KnowledgeMoreFunctionAdapter extends BaseQuickAdapter<NavigationEntity, BaseViewHolderEx> {
    private Context mContext;
    private int type = 0;//0默认状态 1显示是否选中状态2 黑色主题
    private ArrayList<NavigationEntity> data;

    public KnowledgeMoreFunctionAdapter(int layoutResId, Context context, @io.reactivex.annotations.Nullable List<NavigationEntity> data, int type) {
        super(layoutResId, data);
        this.mContext = context;
        this.type = type;
    }


    @Override
    protected void convert(BaseViewHolderEx helper, NavigationEntity entity) {
        ImageView ivNavigation = helper.getView(R.id.ivNavigation);
        LinearLayout llBg = helper.getView(R.id.llBg);
        TextView tvNavigation = helper.getView(R.id.tvNavigation);
        ImageView ivType = helper.getView(R.id.ivType);
        ImageView ivCircle = helper.getView(R.id.ivCircle);

        if (type == 0 || type == 2) {
            ivType.setVisibility(View.GONE);
        } else {
            if (entity.isSelect()) {
                Glide.with(mContext).load(R.drawable.jx_complete).into(ivType);
            } else {
                Glide.with(mContext).load(R.drawable.jx_add).into(ivType);
            }
            ivType.setVisibility(View.VISIBLE);
        }
        tvNavigation.setText(entity.getNavigationName());
        if (type == 2) {
            //首页黑色主题
            LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) llBg.getLayoutParams();
            params.height = Utils.dip2px(mContext, 48);
            llBg.setLayoutParams(params);
            if (Utils.changeNumber(entity.getNum()) > 0) {
                ivCircle.setVisibility(View.VISIBLE);
            } else {
                ivCircle.setVisibility(View.GONE);
            }
            GlideUtils.load(mContext, entity.getToolsImg(), ivNavigation, R.drawable.default_news);
        } else {
            ivCircle.setVisibility(View.GONE);
            GlideUtils.load(mContext, entity.getNavigationImg(), ivNavigation, R.drawable.default_news);
        }
        ivType.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Urls.antiShake.check()) {
                    return;
                }
                if (entity.isSelect()) {
                    saveUseRecord("0", entity.getNavigationCode(), helper.getItemPosition());
                } else {
                    saveUseRecord("1", entity.getNavigationCode(), helper.getItemPosition());
                }

            }
        });
        helper.getItemView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //12写日志
                // 01公告 02 日志 03资料 04学习计划05考试 06 面试 07 通讯录 08  个人能力09 我的待办10 发布视频11 发布新闻12发布日志13题库14视频15溯源
                //    16图文发布17图文18邀请注册19 小视频  20 面试评价 21写日报  22写周报 23写月报24知识库25发布图库26图库27考勤28请假29电子工牌
                Intent intent = null;
                if ("01".equals(entity.getNavigationCode())) {
                    intent = new Intent(mContext, NavigationBaseActivity.class);
                    intent.putExtra("type", 1);
                    mContext.startActivity(intent);
                } else if ("02".equals(entity.getNavigationCode())) {
                    intent = new Intent(mContext, NavigationBaseActivity.class);
                    intent.putExtra("type", 2);
                    mContext.startActivity(intent);
                } else if ("03".equals(entity.getNavigationCode())) {
                    intent = new Intent(mContext, NavigationBaseActivity.class);
                    intent.putExtra("type", 3);
                    mContext.startActivity(intent);
                } else if ("04".equals(entity.getNavigationCode())) {
                    intent = new Intent(mContext, NavigationBaseActivity.class);
                    intent.putExtra("type", 4);
                    mContext.startActivity(intent);
                } else if ("05".equals(entity.getNavigationCode())) {
                    intent = new Intent(mContext, NavigationBaseActivity.class);
                    intent.putExtra("type", 5);
                    mContext.startActivity(intent);
                } else if ("06".equals(entity.getNavigationCode())) {
                    intent = new Intent(mContext, NavigationBaseActivity.class);
                    intent.putExtra("type", 6);
                    mContext.startActivity(intent);
                } else if ("07".equals(entity.getNavigationCode())) {
                    intent = new Intent(mContext, NavigationBaseActivity.class);
                    intent.putExtra("type", 7);
                    mContext.startActivity(intent);
                } else if ("08".equals(entity.getNavigationCode())) {
                    intent = new Intent(mContext, WebCommonActivity.class);
                    String url = NavigationTools.getAllUrl2(mContext, Urls.getCapacityAnalysisCapacity) + "&employeeCode=" + new ShareUtils().getString(mContext, StaticClass.EMPLOYEECODE, "");
                    if (url != null) {
                        intent.putExtra("url", url);
                        intent.putExtra("isPersonalAbility", true);
                        intent.putExtra("title", "个人能力分析");
                        mContext.startActivity(intent);
                    }
                } else if ("09".equals(entity.getNavigationCode())) {
                    intent = new Intent(mContext, WorksActivity.class);
                    mContext.startActivity(intent);
                } else if ("10".equals(entity.getNavigationCode())) {
                    if (Utils.isNull(MyApp.accessKeyId)) {
                        MyToast.show("暂无权限上传视频");
                    } else {
                        if (type == 0) {
                            EventBus.getDefault().post(new MyEvent("KnowledgeMoreReleasedVideo"));
                        } else if (type == 2) {
                            EventBus.getDefault().post(new MyEvent("HomeReleasedVideo"));
                        }
                    }
                } else if ("11".equals(entity.getNavigationCode())) {
                    intent = new Intent(mContext, PublishNewsActivity.class);
                    mContext.startActivity(intent);
                } else if ("12".equals(entity.getNavigationCode())) {
                    intent = new Intent(mContext, PublishLogsActivity.class);
                    intent.putExtra("type", 3);
                    mContext.startActivity(intent);
                } else if ("21".equals(entity.getNavigationCode())) {
                    intent = new Intent(mContext, PublishLogsActivity.class);
                    intent.putExtra("type", 0);
                    mContext.startActivity(intent);
                } else if ("22".equals(entity.getNavigationCode())) {
                    intent = new Intent(mContext, PublishLogsActivity.class);
                    intent.putExtra("type", 1);
                    mContext.startActivity(intent);
                } else if ("23".equals(entity.getNavigationCode())) {
                    intent = new Intent(mContext, PublishLogsActivity.class);
                    intent.putExtra("type", 2);
                    mContext.startActivity(intent);
                } else if ("13".equals(entity.getNavigationCode())) {
                    intent = new Intent(mContext, NavigationBaseActivity.class);
                    intent.putExtra("type", 0);
                    mContext.startActivity(intent);
                } else if ("14".equals(entity.getNavigationCode())) {
                    intent = new Intent(mContext, VideoBaseActivity.class);
                    mContext.startActivity(intent);
                } else if ("15".equals(entity.getNavigationCode())) {
                    intent = new Intent(mContext, NavigationBaseActivity.class);
                    intent.putExtra("type", 15);
                    mContext.startActivity(intent);
                } else if ("16".equals(entity.getNavigationCode())) {
                    intent = new Intent(mContext, PublishTuwensActivity.class);
                    mContext.startActivity(intent);
                } else if ("17".equals(entity.getNavigationCode())) {
                    intent = new Intent(mContext, NavigationBaseActivity.class);
                    intent.putExtra("type", 17);
                    mContext.startActivity(intent);
                } else if ("18".equals(entity.getNavigationCode())) {
                    intent = new Intent(mContext, InvitationRegisterActivity.class);
                    mContext.startActivity(intent);
                } else if ("19".equals(entity.getNavigationCode())) {
                    intent = new Intent(mContext, VideoBaseActivity.class);
                    intent.putExtra("index", 1);
                    mContext.startActivity(intent);
                } else if ("20".equals(entity.getNavigationCode())) {
                    intent = new Intent(mContext, InterviewRecordsActivity.class);
                    intent.putExtra("type", 2);
                    mContext.startActivity(intent);
                } else if ("24".equals(entity.getNavigationCode())) {
                    intent = new Intent(mContext, NavigationBaseActivity.class);
                    intent.putExtra("type", 24);
                    mContext.startActivity(intent);
                } else if ("25".equals(entity.getNavigationCode())) {
                    if (type == 0) {
                        EventBus.getDefault().post(new MyEvent("KnowledgeMoreReleasedGallery"));
                    } else if (type == 2) {
                        EventBus.getDefault().post(new MyEvent("HomeReleasedGallery"));
                    }
                } else if ("26".equals(entity.getNavigationCode())) {
                    intent = new Intent(mContext, NavigationBaseActivity.class);
                    intent.putExtra("type", 26);
                    mContext.startActivity(intent);
                } else if ("27".equals(entity.getNavigationCode())) {
                    intent = new Intent(mContext, ClockInHomeActivity.class);
                    mContext.startActivity(intent);
                } else if ("28".equals(entity.getNavigationCode())) {
                    intent = new Intent(mContext, ClockInAskHolidayActivity.class);
                    mContext.startActivity(intent);
                } else if ("29".equals(entity.getNavigationCode())) {
                    intent = new Intent(mContext, WorkCardActivity.class);
                    mContext.startActivity(intent);
                } else if ("30".equals(entity.getNavigationCode())) {
                    intent = new Intent(mContext, ReimbursementActivity.class);
                    mContext.startActivity(intent);
                } else if ("31".equals(entity.getNavigationCode())) {
                    intent = new Intent(mContext, ShopActivity.class);
                    mContext.startActivity(intent);
                } else if ("32".equals(entity.getNavigationCode())) {
                    intent = new Intent(mContext, ReimbursementActivity.class);
                    intent.putExtra("type", 9);
                    mContext.startActivity(intent);
                } else if ("33".equals(entity.getNavigationCode())) {
                    intent = new Intent(mContext, ReimbursementActivity.class);
                    intent.putExtra("type", 10);
                    mContext.startActivity(intent);
                } else if ("34".equals(entity.getNavigationCode())) {
                    intent = new Intent(mContext, RunningHomeActivity.class);
                    mContext.startActivity(intent);
                } else if ("35".equals(entity.getNavigationCode())) {
                    intent = new Intent(mContext, SealHomeActivity.class);
                    mContext.startActivity(intent);
                } else if ("36".equals(entity.getNavigationCode())) {
                    intent = new Intent(mContext, InspectionHomeActivity.class);
                    mContext.startActivity(intent);
                } else if ("37".equals(entity.getNavigationCode())) {
                    intent = new Intent(mContext, PaySlipActivity.class);
                    mContext.startActivity(intent);
                }else if ("38".equals(entity.getNavigationCode())) {
                    intent = new Intent(mContext, ResignHomeActivity.class);
                    mContext.startActivity(intent);
                }else if ("39".equals(entity.getNavigationCode())) {
                    intent = new Intent(mContext, UpgradeHomeActivity.class);
                    mContext.startActivity(intent);
                }else if ("40".equals(entity.getNavigationCode())) {
                    intent = new Intent(mContext, ProjectManagementActivity.class);
                    mContext.startActivity(intent);
                }else if("41".equals(entity.getNavigationCode())) {
                    intent = new Intent(mContext, OvertimeActivity.class);
                    mContext.startActivity(intent);
                }
                if (type == 0 || type == 1) {
                    try {
                        if (Activity.class.isInstance(mContext)) {
                            // 转化为activity，然后finish就行了
                            Activity activity = (Activity) mContext;
                            activity.finish();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }

    public void saveUseRecord(String statusCd, String navigationCode, int position) {
        //状态（0 取消/1 添加）
        Map<String, String> map = new HashMap<>();
        map.put("statusCd", statusCd);
        map.put("navigationCode", navigationCode);
        MyHttpRequest.postRequest(Urls.setNavigation, this, SignUtils2.makeSignStr(map), new JsonCallback<String>() {
            @Override
            public void onSuccess(Response<String> response) {
                try {
                    JSONObject jsonObject = new JSONObject(response.body().toString());
                    if ("200".equals(jsonObject.getString("code"))) {
                        if (statusCd.equals("0")) {
                            getData().get(position).setSelect(false);
                        } else {
                            getData().get(position).setSelect(true);
                            MyToast.show("已添加到首页导航");
                        }
                        notifyItemChanged(position);
                    } else {
                        if (jsonObject.getString("code").startsWith("400")) {
                            OkGoUtil.onErrorIntent(jsonObject.getString("code"), jsonObject.getString("msg"));
                        } else {
                            MyToast.show(jsonObject.getString("msg"));
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
